<?php
/**
 * Description of Bd
 *
 * @author inti
 */
class Bd extends mysqli {

  private $servidor = "";
  private $usuario = "";
  private $clave = "";
  private $baseDatos = "";
  public static $IDENTITY = -1;

  const QUERY = 0;
  const MULTI_QUERY = 1;
  const SCALAR = 2;
  const AMBIENTE = "DESARROLLO";
  const ORIGEN_DATOS_DEFAULT = 0;
  const ORIGEN_DATOS_OTRO = 1;

  public function __construct($origenDatos) {
    switch ($origenDatos . "_" . self::AMBIENTE) {    
      case self::ORIGEN_DATOS_DEFAULT . "_PRODUCCION":
        $this->servidor = "diosasdelplacer.cl";
        $this->usuario = "diosasde_admin";
        $this->clave = "qpwozmxn";
        $this->baseDatos = "diosasde_alimentasalud";
        break;
      case self::ORIGEN_DATOS_DEFAULT . "_DESARROLLO":
        $this->servidor = "localhost";
        $this->usuario = "root";
        $this->clave = "qpwozmxn";
        //$this->clave = "";
        $this->baseDatos = "alimentasalud";
        break;
      default:
        $this->servidor = "";
        $this->usuario = "";
        $this->clave = "";
        $this->basedatos = "";
        break;
    }

    @parent:: __construct($this->servidor, $this->usuario, $this->clave);
    if ($this->connect_errno != 0) {
        throw new Exception($bd->error, $bd->errno);
    }
  }

  private static function get($origenDatos) {
    $bd = new Bd($origenDatos);
    $bd->select_db($bd->baseDatos);
    return $bd;
  }

  /**
   *
   * @param string $sp
   * @param enum $tipo
   * @param string $origenDatos
   * @param string $nombreCampoValor
   * @return array 
   */
  public static function exec($sp, $tipo = self::QUERY, $origenDatos = self::ORIGEN_DATOS_DEFAULT, $nombreCampoValor = "Valor") {
    $bd = self::get($origenDatos);

    $result = null;
        switch ($tipo) {
            case self::QUERY:
                $result = $bd->query($sp);
                // SI LA QUERY CONTIENE INSERT GUARDA EL ID INSERTADO
                
                if (strpos($sp, "INSERT") >= 0) {
                    self::$IDENTITY = $bd->insert_id;
                }
                if ($bd->errno != 0) {
                    throw new Exception($bd->error, $bd->errno);
                }
                $array = array();
                try {
                    while ($row = mysqli_fetch_assoc($result)) {
                        $array[] = $row;
                    }
                } catch (Exception $exc) {
                    
                }
                $bd->close();
                return $array;
                break;
            case self::MULTI_QUERY:
                $result = $bd->multi_query($sp);
                $arrTablas = array();
                if ($bd->errno != 0) {
                    throw new Exception($bd->error, $bd->errno);
                }
                do {
                    $cur = $bd->store_result();
                    if ($cur) {
                        $array = array();
                        while ($row = mysqli_fetch_assoc($cur)) {
                            $array[] = $row;
                        }
                        mysqli_free_result($cur);
                        $arrTablas[] = $array;
                    }
                } while ($bd->next_result());
                $bd->close();
                return $arrTablas;
                break;
            case self::SCALAR:
                $result = $bd->query($sp);
                if ($bd->errno != 0) {
                    throw new Exception($bd->error, $bd->errno);
                }
                $array = array();
                while ($row = mysqli_fetch_assoc($result)) {
                    $array[] = $row;
                }
                $row = $array[0];
                $valor = ( is_null($row["$nombreCampoValor"]) ) ? "" : $row["$nombreCampoValor"];
                $bd->close();
                return $valor;
                break;
            default:
                return null;
                break;
        }
    }
}

?>
